package main

import (
	"log"
	"syscall"
	"unsafe"
)

var _ unsafe.Pointer

var (
	modwinspool = syscall.NewLazyDLL("winspool.drv")

	procGetDefaultPrinterW = modwinspool.NewProc("GetDefaultPrinterW")
	procClosePrinter       = modwinspool.NewProc("ClosePrinter")
	procOpenPrinterW       = modwinspool.NewProc("OpenPrinterW")
	procStartDocPrinterW   = modwinspool.NewProc("StartDocPrinterW")
	procEndDocPrinter      = modwinspool.NewProc("EndDocPrinter")
	procWritePrinter       = modwinspool.NewProc("WritePrinter")
	procStartPagePrinter   = modwinspool.NewProc("StartPagePrinter")
	procEndPagePrinter     = modwinspool.NewProc("EndPagePrinter")
	procEnumPrintersW      = modwinspool.NewProc("EnumPrintersW")
	procGetPrinterDriverW  = modwinspool.NewProc("GetPrinterDriverW")
)

func main() {
	b := make([]uint16, 30)
	n := uint32(len(b))
	err := GetDefaultPrinter(&b[0], &n)
	if err != nil {
		if err != syscall.ERROR_INSUFFICIENT_BUFFER {
			log.Print(err)
		}
		b = make([]uint16, n)
		err = GetDefaultPrinter(&b[0], &n)
		if err != nil {
			log.Print(err)
		}
	}
	log.Println(syscall.UTF16ToString(b))
	syscall.Syscall(procOpenPrinterW.Addr(), 3, uintptr(unsafe.Pointer(name)), uintptr(unsafe.Pointer(h)), uintptr(defaults))

	ssyscall.Syscall(procClosePrinter.Addr(), 1, uintptr(h), 0, 0)
}

func GetDefaultPrinter(buf *uint16, bufN *uint32) (err error) {
	r1, _, e1 := syscall.Syscall(procGetDefaultPrinterW.Addr(), 2, uintptr(unsafe.Pointer(buf)), uintptr(unsafe.Pointer(bufN)), 0)
	if r1 == 0 {
		if e1 != 0 {
			err = error(e1)
		} else {
			err = syscall.EINVAL
		}
	}
	return
}
